package cn.iocoder.yudao.module.handguard.controller.app.usermessage;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.handguard.controller.app.usermessage.vo.message.NotifyMessageRespVO;
import cn.iocoder.yudao.module.handguard.convert.messages.MessagesConvert;
import cn.iocoder.yudao.module.handguard.dal.dataobject.messages.MessagesDO;
import cn.iocoder.yudao.module.handguard.dal.mysql.messages.MessagesMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUser;

/**
 * @author: liuweijun
 * @since: 2023/9/19 14:19
 * @description:
 */
@Tag(name = "小程序 - 用户消息通知")
@RestController
@RequestMapping("/message")
@Validated
public class MessagesController {

    @Resource
    private MessagesMapper messagesMapper;

    @GetMapping("/list")
    @Operation(summary = "获取消息通知列表")
    public CommonResult<List<NotifyMessageRespVO>> getUnreadNotifyMessageList() {
        Long userId = getLoginUser().getId();
        List<MessagesDO> list = messagesMapper.getList(userId);
        List<NotifyMessageRespVO> respVOList = MessagesConvert.INSTANCE.convertList(list);
        for (NotifyMessageRespVO m : respVOList){
           String name = messagesMapper.selectDictByValue(m.getTemplateType());
           m.setTitle(name);
        }
        return success(respVOList);
    }

    @GetMapping("/messageNoReadNumber")
    @Operation(summary = "我的-未读消息数量")
    public CommonResult<Long> messageNoReadNumber() {
        Long userId = getLoginUser().getId();
        LambdaQueryWrapperX<MessagesDO> wrapperX = new LambdaQueryWrapperX<>();
        wrapperX.eq(MessagesDO::getUserId,userId);
        wrapperX.eq(MessagesDO::getReadStatus,false);
        Long count =  messagesMapper.selectCount(wrapperX);
        return success(count);
    }


    @PostMapping("/messageRead")
    @Operation(summary = "设置消息已读状态")
    public CommonResult<Boolean> messageRead(@RequestParam("mid") Long mid) {
        Long userId = getLoginUser().getId();
        MessagesDO message =  messagesMapper.selectById(mid);
        if (!message.getReadStatus()){
            message.setReadStatus(true);
            message.setReadTime(LocalDateTime.now());
            int flag = messagesMapper.updateById(message);
        }
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获取消息详情")
    public CommonResult<NotifyMessageRespVO> get(@RequestParam("id") Long id) {
        MessagesDO info = messagesMapper.getInfo(id);
        NotifyMessageRespVO respVO = MessagesConvert.INSTANCE.convert(info);
        if (respVO!=null){
            String name = messagesMapper.selectDictByValue(respVO.getTemplateType());
            respVO.setTitle(name);
        }
        //标记已读
        messagesMapper.signRead(id);

        return success(respVO);
    }

}
